package com.lc.cehui.ui.activity;

import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.blankj.utilcode.util.LogUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.donkingliang.labels.LabelsView;
import com.google.android.material.imageview.ShapeableImageView;
import com.hjq.bar.OnTitleBarListener;
import com.hjq.bar.TitleBar;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.cehui.R;
import com.lc.cehui.app.AppActivity;
import com.lc.cehui.http.glide.GlideApp;
import com.lc.cehui.http.request.CommentList;
import com.lc.cehui.http.response.CommentListBean;
import com.scwang.smart.refresh.header.MaterialHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

public class CommentListActivity extends AppActivity {

    @BindView(R.id.titleBar)
    TitleBar titleBar;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    BaseQuickAdapter adapter;
    List<CommentListBean.DataBean> lists = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_comment_list;
    }

    @Override
    protected void initView() {
        titleBar.setOnTitleBarListener(new OnTitleBarListener() {
            @Override
            public void onLeftClick(View v) {
                finish();
            }

            @Override
            public void onTitleClick(View v) {

            }

            @Override
            public void onRightClick(View v) {

            }
        });
        refreshLayout.setRefreshHeader(new MaterialHeader(CommentListActivity.this));
        recyclerView.setLayoutManager(new LinearLayoutManager(CommentListActivity.this));
        recyclerView.setAdapter(adapter = new BaseQuickAdapter<CommentListBean.DataBean,BaseViewHolder>(R.layout.adapter_commodity_review,lists) {
            @Override
            protected void convert(@NonNull BaseViewHolder holder, CommentListBean.DataBean dataBean) {
                holder.setText(R.id.tv_comment_name,dataBean.getNickname()).setText(R.id.tv_comment_body,dataBean.getText()).setText(R.id.tv_comment_attr,dataBean.getAttr());
                GlideApp.with(CommentListActivity.this).load(dataBean.getPicurl()).into((ShapeableImageView) holder.getView(R.id.iv_comment_logo));
                LabelsView labelsView = holder.getView(R.id.labelsView);
                labelsView.setLabels(dataBean.getLable(), new LabelsView.LabelTextProvider<CommentListBean.DataBean.LableBean>() {
                    @Override
                    public CharSequence getLabelText(TextView label, int position, CommentListBean.DataBean.LableBean data) {
                        return data.getName();
                    }
                });

            }
        });
        showLoadView(adapter);
    }

    @Override
    protected void initData() {
        EasyHttp.post(this)
                .api(new CommentList().setPage(1)
                        .setGoods_id(getIntent().getIntExtra("goods_id", 1))
                ).request(new HttpCallback<CommentListBean>(this) {
            @Override
            public void onSucceed(CommentListBean data) {
                LogUtils.e(JSON.toJSONString(data));
                if (callBackCode(data)){
                    if (data.getData().size()!=0){
                        lists.addAll(data.getData());
                    }
                    adapter.notifyDataSetChanged();
                }
            }
        });

    }
}